package com.vtech.ont.base;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.NsIterator;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.*;
import org.springframework.stereotype.Service;

/**
 * <pre/>本体前缀处理
 * 非SPARQL中的前缀
 *
 * @author houzhiwei
 * @date 2017/6/29 15:12
 */
@Slf4j
@Service
public class OntPrefixUtil {
    static String[][] preNS = new String[][]{
            {"rdfs", RDFS.getURI()},
            {"rdf", RDF.getURI()},
            {"owl", OWL2.getURI()},
            {"xsd", XSD.getURI()},
            {"rss", RSS.getURI()},
            {"vcard", VCARD.getURI()},
            {"foaf", Consts.NS_FOAF},
            {"swrl", Consts.NS_SWRL},
            {"fn", Consts.NS_FN},
            {"sfn", Consts.NS_SPARQL}};

    public static OntModel registerPrefixes(OntModel model) {
        model = registerOntPrefixes(model);
        NsIterator nsIter = model.listNameSpaces();
        String ns = null, pre = null;
        while (nsIter.hasNext()) {
            ns = nsIter.next();
            if (StringUtils.isBlank(model.getNsURIPrefix(ns))) {
                pre = getPrefixStr(ns);
                if (!StringUtils.isBlank(pre))
                    model.setNsPrefix(pre, ns);// 设置命名空间前缀
            }
        }
        return model;
    }

    public static OntModel registerOntPrefixes(OntModel model) {
        for (int i = 0, leni = preNS.length; i < leni; i++) {
            model.setNsPrefix(preNS[i][0], preNS[i][1]);
        }
        return model;
    }

    public static String getPrefixStr(String ns) {
        String pre = null;
        try {
            if (ns.indexOf('#') > -1) {
                ns = ns.substring(0, ns.lastIndexOf('#') + 1); // 命名空间
                pre = ns.substring(ns.lastIndexOf('/') + 1, ns.indexOf('#'));// 取可能的前缀
                try {
                    Long l = Long.parseLong(pre); //是否为数字（版本信息）
                    ns = ns.substring(0, ns.lastIndexOf('/')) + "#";//构造新命名空间
                    pre = getPrefixStr(ns); //再次获取前缀
                } catch (Exception e) {
//                    不是数字时
                    int owl = pre.indexOf(".owl");
                    if (owl > -1)// 去掉.owl
                        pre = pre.substring(0, owl);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getLocalizedMessage());
        }
        return pre;
    }

    public static String setResPrefix(Resource resource) {
        String ns = resource.getNameSpace();
        String prefix = null;
        Model resModel = resource.getModel();
        if (resModel != null && ns != null) {
            // resNS=resNS.substring(0,resNS.lastIndexOf("#")+1);//将#后面的名称去掉，这样即使后面有/也不用担心下一行报错
            String pre = getPrefixStr(ns);
            if (StringUtils.isBlank(pre))
                return null;
            try {
                resModel.setNsPrefix(pre, ns);
                prefix = resModel.getNsURIPrefix(ns);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                // e.printStackTrace();
                return pre;
            }
        }
        return prefix;
    }
}
